package me.keiwu.user.controller;

import com.alibaba.fastjson.JSONObject;
import me.keiwu.pub.data.Response;
import me.keiwu.pub.data.wx.WXSessionData;
import me.keiwu.pub.data.wx.WXStepData;
import me.keiwu.pub.service.WXService;
import me.keiwu.pub.util.CacheUtil;
import me.keiwu.pub.util.ContextUtil;
import me.keiwu.pub.util.DateUtil;
import me.keiwu.user.domain.UserStep;
import me.keiwu.user.domain.UserTotalStep;
import me.keiwu.user.service.StepService;
import me.keiwu.user.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by kei on 25/11/2017.
 */
@RestController
@RequestMapping("/step")
public class StepController {


    private Logger logger = LoggerFactory.getLogger(StepController.class);

    @Autowired
    private WXService wxService;
    @Autowired
    private StepService stepService;
    @Autowired
    private CacheUtil cacheUtil;
    @Autowired
    private UserService userService;


    @ResponseBody
    @RequestMapping("/commit")
    public Response commit(HttpServletRequest request, String encryptedData, String iv) {
        Long userId = ContextUtil.getLoginUserId(request);

        WXSessionData session = cacheUtil.getWXSession(userId);
        if (session == null) return Response.fail(-2, "not login");

        WXStepData data = wxService.decryptData(encryptedData, iv, session.getSessionKey(), WXStepData.class);
        if (data == null) {
            logger.error("commit steps fail! data is null");
            return Response.fail("获取不了步数");
        }
        List<UserStep> userSteps = stepService.saveStepData(userId, data);
        int days = DateUtil.getDayStrInt(new Date());
        UserTotalStep totalStep = userService.getUserTotalStep(userId);
        UserStep dailyStep = null;
        for (UserStep step : userSteps) {
            if (days == step.getDays()) {
                logger.info("commit ok! dailyStep={}", step);
                dailyStep = step;
                break;
            }
        }
        if (dailyStep == null) {
            logger.error("commit steps fail2! data is null");
            return Response.fail("获取不了步数");
        }
        JSONObject json = new JSONObject();
        json.put("dailyStep", dailyStep);
        json.put("totalStep", totalStep.getTotalStep() + dailyStep.getStep());
        json.put("totalStep30", totalStep.getTotalStep30());
        logger.info("commit step return: json={}", json);
        return Response.ok(json);
    }

    @ResponseBody
    @RequestMapping("/exchange")
    public Response exchange(HttpServletRequest request) {
        Long userId = ContextUtil.getLoginUserId(request);
        return stepService.exchangeStep(userId);
    }

}
